//
//  TimeSpread.m
//  SujetObserver
//
//  Created by Pierre DUCHENE on 05/03/10.
//  Copyright 2010 Pierre DUCHENE. All rights reserved.
//

#import "VersionSpread.h"
#import "SensorData.h"


@implementation VersionSpread

- (id) init
{
	self = [super init];
	if (self != nil) {
		currentVersion = 0;
	}
	return self;
}

- (void)execute
{
	// We notify all observers conccurently
	NSSet* observers = [sensor getAllObservers];
	NSInvocationOperation* io = nil;
	currentVersion++;
	for (id<ObserverProtocol> o in observers) {
		io = [[NSInvocationOperation alloc] initWithTarget:o
												  selector:@selector(update:)
													object:sensor];
		// Each update operation must wait until the new value will set
		[updateQueue addOperation:io];
		[io release];
		io = nil;
	}
}

- (SensorData*)sensorData
{
	SensorData* result =[sensor dataForContext];
	[result setDataVersion:currentVersion];
	return result;
}

@end
